﻿namespace Sage.FileWriter.Extensions
{
    /// <summary>
    /// StringFileWriter 扩展方法
    /// </summary>
    public static class StringFileWriterExtensions
    {
        /// <summary>
        /// 快速保存字符串到文件
        /// </summary>
        /// <param name="content">字符串内容</param>
        /// <param name="filePath">文件路径</param>
        /// <param name="existsAction">文件存在时的处理策略</param>
        /// <param name="encoding">编码格式，默认为UTF-8</param>
        /// <returns>文件写入结果</returns>
        public static async Task<FileWriteResult> QuickSaveAsync(string content, string filePath,
            FileExistsAction existsAction = FileExistsAction.Overwrite,
            FileEncoding encoding = FileEncoding.UTF8)
        {
            return await StringFileWriter
                .Content(content, encoding)
                .Path(filePath)
                .OnFileExists(_ => existsAction)
                .SaveAsync();
        }

        /// <summary>
        /// 字符串扩展方法，保存到文件
        /// </summary>
        /// <param name="content">字符串内容</param>
        /// <param name="filePath">文件路径</param>
        /// <param name="encoding">编码格式，默认为UTF-8</param>
        /// <returns>文件写入结果</returns>
        public static async Task<FileWriteResult> SaveToFileAsync(this string content, string filePath,
            FileEncoding encoding = FileEncoding.UTF8)
        {
            return await StringFileWriter
                .Content(content, encoding)
                .Path(filePath)
                .SaveAsync();
        }
    }
}